/**
 * 表单字段类型库
 * 集中管理所有字段类型定义
 */

// 字段类型键名常量
export const FIELD_TYPE = {
  // 基础类型
  TEXT: 'text',
  NUMBER: 'number',
  RADIO: 'radio',
  CHECKBOX: 'checkbox',
  IMAGE: 'image',
  VIDEO: 'video',
  AUDIO: 'audio',
  FILE: 'file',
  DATETIME: 'datetime',
  DATE: 'date',
  TIME: 'time',
  GEO: 'geo',
  
  // 预设类型
  NAME: 'name',
  PHONE: 'phone',
  IDCARD: 'idcard',
  QUANTITY: 'quantity',
  AMOUNT: 'amount',
  LOCATION: 'location',
  ADDRESS: 'address',
  REGION: 'region',
  DEPARTMENT: 'department',
  REMARK: 'remark',
  // SIGNATURE: 'signature'
}

// 按分类组织的字段类型
export const FIELD_CATEGORIES = {
  COMMON: [
    FIELD_TYPE.NAME,
    FIELD_TYPE.PHONE,
    FIELD_TYPE.TIME,
    FIELD_TYPE.DATE,
    FIELD_TYPE.ADDRESS,
    FIELD_TYPE.IDCARD,
    FIELD_TYPE.LOCATION,
    FIELD_TYPE.AMOUNT,
    FIELD_TYPE.QUANTITY,
    FIELD_TYPE.REGION,
    FIELD_TYPE.DEPARTMENT,
    FIELD_TYPE.REMARK,
  ],
  CUSTOM: [
    FIELD_TYPE.TEXT,
    FIELD_TYPE.NUMBER,
    FIELD_TYPE.RADIO,
    FIELD_TYPE.CHECKBOX,
    FIELD_TYPE.IMAGE,
    FIELD_TYPE.VIDEO,
    FIELD_TYPE.AUDIO,
    FIELD_TYPE.FILE,
    FIELD_TYPE.DATETIME,
    FIELD_TYPE.GEO,
    // FIELD_TYPE.SIGNATURE,
  ]
}

// 完整字段类型配置
export const FIELD_CONFIG = {
  // 基础类型
  [FIELD_TYPE.TEXT]: {
    key: FIELD_TYPE.TEXT,
    baseType: FIELD_TYPE.TEXT,
    label: '文本',
    icon: 'font',
    placeholder: '请输入文本',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.NUMBER]: {
    key: FIELD_TYPE.NUMBER,
    baseType: FIELD_TYPE.NUMBER,
    label: '数字',
    icon: 'upload',
    placeholder: '请输入数字',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.RADIO]: {
    key: FIELD_TYPE.RADIO,
    baseType: FIELD_TYPE.RADIO,
    label: '单选',
    icon: 'circle',
    defaultSettings: {
      required: true,
      isPublic: true,
      options: ['选项1', '选项2']
    }
  },
  
  [FIELD_TYPE.CHECKBOX]: {
    key: FIELD_TYPE.CHECKBOX,
    baseType: FIELD_TYPE.CHECKBOX,
    label: '多选',
    icon: 'checkbox',
    defaultSettings: {
      required: true,
      isPublic: true,
      options: ['选项1', '选项2'],
      maxSelect: 0
    }
  },
  
  [FIELD_TYPE.DATE]: {
    key: FIELD_TYPE.DATE,
    baseType: FIELD_TYPE.DATETIME,
    label: '日期',
    icon: 'calendar',
    defaultSettings: {
      required: true,
      isPublic: true,
      format: 'date'
    }
  },
  
  [FIELD_TYPE.TIME]: {
    key: FIELD_TYPE.TIME,
    baseType: FIELD_TYPE.DATETIME,
    label: '时间',
    icon: 'calendar',
    defaultSettings: {
      required: true,
      isPublic: true,
      format: 'time'
    }
  },
  
  [FIELD_TYPE.DATETIME]: {
    key: FIELD_TYPE.DATETIME,
    baseType: FIELD_TYPE.DATETIME,
    label: '日期/时间',
    icon: 'calendar',
    defaultSettings: {
      required: true,
      isPublic: true,
      format: 'year'
    }
  },
  
  [FIELD_TYPE.IMAGE]: {
    key: FIELD_TYPE.IMAGE,
    baseType: FIELD_TYPE.IMAGE,
    label: '图片',
    icon: 'image',
    defaultSettings: {
      required: true,
      isPublic: true,
      uploadMethods: ['camera', 'album'],
      imageLimit: 1
    }
  },
  
  [FIELD_TYPE.VIDEO]: {
    key: FIELD_TYPE.VIDEO,
    baseType: FIELD_TYPE.VIDEO,
    label: '视频',
    icon: 'videocam',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.AUDIO]: {
    key: FIELD_TYPE.AUDIO,
    baseType: FIELD_TYPE.AUDIO,
    label: '录音',
    icon: 'mic',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.FILE]: {
    key: FIELD_TYPE.FILE,
    baseType: FIELD_TYPE.FILE,
    label: '文件',
    icon: 'folder-add',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.GEO]: {
    key: FIELD_TYPE.GEO,
    baseType: FIELD_TYPE.GEO,
    label: '定位',
    icon: 'location',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  // 预设类型
  [FIELD_TYPE.NAME]: {
    key: FIELD_TYPE.NAME,
    baseType: FIELD_TYPE.TEXT,
    label: '姓名',
    icon: 'person',
    placeholder: '请输入姓名',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.PHONE]: {
    key: FIELD_TYPE.PHONE,
    baseType: FIELD_TYPE.PHONE,
    label: '手机号',
    icon: 'phone',
    placeholder: '请输入手机号',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.IDCARD]: {
    key: FIELD_TYPE.IDCARD,
    baseType: FIELD_TYPE.IDCARD,
    label: '身份证',
    icon: 'paperclip',
    placeholder: '请输入身份证号',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.QUANTITY]: {
    key: FIELD_TYPE.QUANTITY,
    baseType: FIELD_TYPE.NUMBER,
    label: '数量',
    icon: 'upload',
    placeholder: '请输入数量',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.AMOUNT]: {
    key: FIELD_TYPE.AMOUNT,
    baseType: FIELD_TYPE.NUMBER,
    label: '金额',
    icon: 'upload',
    placeholder: '请输入金额',
    defaultSettings: {
      required: true,
      isPublic: true,
      limitRange: false
    }
  },
  
  [FIELD_TYPE.LOCATION]: {
    key: FIELD_TYPE.LOCATION,
    baseType: FIELD_TYPE.GEO,
    label: '定位',
    icon: 'location',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.ADDRESS]: {
    key: FIELD_TYPE.ADDRESS,
    baseType: FIELD_TYPE.TEXT,
    label: '地址',
    icon: 'location',
    placeholder: '请输入地址',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.REGION]: {
    key: FIELD_TYPE.REGION,
    baseType: FIELD_TYPE.REGION,
    label: '省/市/区',
    icon: 'location',
    defaultSettings: {
      required: true,
      isPublic: true,
      regionFormat: 'province-city-district'
    }
  },
  
  [FIELD_TYPE.DEPARTMENT]: {
    key: FIELD_TYPE.DEPARTMENT,
    baseType: FIELD_TYPE.TEXT,
    label: '部门',
    icon: 'staff',
    placeholder: '请输入部门',
    defaultSettings: {
      required: true,
      isPublic: true
    }
  },
  
  [FIELD_TYPE.REMARK]: {
    key: FIELD_TYPE.REMARK,
    baseType: FIELD_TYPE.TEXT,
    label: '备注',
    icon: 'font',
    placeholder: '请输入备注',
    defaultSettings: {
      required: false,
      isPublic: true
    }
  },
  
  // [FIELD_TYPE.SIGNATURE]: {
  //   key: FIELD_TYPE.SIGNATURE,
  //   baseType: FIELD_TYPE.SIGNATURE,
  //   label: '手写签名',
  //   icon: 'compose',
  //   defaultSettings: {
  //     required: true,
  //     isPublic: true
  //   }
  // }
}

/**
 * 创建字段实例
 * @param {string} type 字段类型
 * @param {number} id 字段ID
 * @param {object} customSettings 自定义设置
 * @returns {object} 字段实例
 */
export function createField(type, id, customSettings = {}) {
  const config = FIELD_CONFIG[type] || FIELD_CONFIG[FIELD_TYPE.TEXT];
  
  return {
    id,
    type,
    title: config.label,
    ...(config.defaultSettings || {}),
    ...customSettings
  };
}

/**
 * 获取字段类型标签
 * @param {string|object} type 字段类型
 * @returns {string} 字段类型标签
 */
export function getFieldTypeLabel(type) {
  if (typeof type === 'object') {
    if (type.label) return type.label;
    if (type.baseType && FIELD_CONFIG[type.baseType]) {
      return FIELD_CONFIG[type.baseType].label;
    }
    return '未知类型';
  }
  
  return FIELD_CONFIG[type]?.label || '未知类型';
}

/**
 * 获取字段类型图标
 * @param {string|object} type 字段类型
 * @returns {string} 字段类型图标
 */
export function getFieldTypeIcon(type) {
  if (typeof type === 'object') {
    if (type.icon) return type.icon;
    if (type.baseType && FIELD_CONFIG[type.baseType]) {
      return FIELD_CONFIG[type.baseType].icon;
    }
    return 'font';
  }
  
  return FIELD_CONFIG[type]?.icon || 'font';
}

/**
 * 获取字段占位符
 * @param {string|object} type 字段类型
 * @returns {string} 字段占位符
 */
export function getFieldPlaceholder(type) {
  if (typeof type === 'object') {
    if (type.placeholder) return type.placeholder;
    if (type.baseType && FIELD_CONFIG[type.baseType]) {
      return FIELD_CONFIG[type.baseType].placeholder || '请输入';
    }
    return '请输入';
  }
  
  return FIELD_CONFIG[type]?.placeholder || '请输入';
} 